'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('categories', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true,
      },
      name: {
        type: Sequelize.STRING(50),
        allowNull: false,
        comment: '分类名称',
      },
      name_en: {
        type: Sequelize.STRING(50),
        allowNull: true,
        comment: '英文名称',
      },
      parent_id: {
        type: Sequelize.INTEGER,
        allowNull: true,
        comment: '父分类ID',
      },
      level: {
        type: Sequelize.INTEGER,
        defaultValue: 1,
        comment: '分类层级',
      },
      sort_order: {
        type: Sequelize.INTEGER,
        defaultValue: 0,
        comment: '排序',
      },
      icon: {
        type: Sequelize.STRING(255),
        allowNull: true,
        comment: '分类图标',
      },
      description: {
        type: Sequelize.TEXT,
        allowNull: true,
        comment: '分类描述',
      },
      is_active: {
        type: Sequelize.BOOLEAN,
        defaultValue: true,
        comment: '是否启用',
      },
      created_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.NOW,
      },
      updated_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.NOW,
      },
    });

    // 添加索引
    await queryInterface.addIndex('categories', ['parent_id']);
    await queryInterface.addIndex('categories', ['level']);
    await queryInterface.addIndex('categories', ['sort_order']);
  },

  down: async (queryInterface, Sequelize) => {
    await queryInterface.dropTable('categories');
  },
};
